package com.menker.app.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.menker.app.Constants;
import com.menker.app.R;
import com.menker.app.biz.WebPageInfo;
import com.menker.app.common.BaseWebViewToolbarFragment;
import com.menker.app.common.Utils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;


/**
 * @author
 * @version 1.0
 */
@EFragment
public class WebViewFragment extends BaseWebViewToolbarFragment {

    @FragmentArg("webPageInfo")
    WebPageInfo webPageInfo;

    @StringRes(R.string.error_no_internet)
    protected String strNoInternet;

    private View mLayoutView;

    private AVLoadingIndicatorView mLoadingView;
    private RelativeLayout mRelativeLayout;
    protected LinearLayout mLayoutSearch;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLayoutView = inflater.inflate(R.layout.frag_webview, container, false);

        setupUI(webPageInfo, ((MainActivity_) getActivity()).getApiHelper(), ((MainActivity_) getActivity()).getCurrentUser());

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        mLayoutWebView.addView(mWebView, layoutParams);

        return mLayoutView;
    }

    @AfterViews
    void init() {
        delayToHideLoadingView();
    }

    @Override
    protected void findViews() {
        mToolbar = (Toolbar) mLayoutView.findViewById(R.id.frag_webview_toolbar);
        mToolbarTitle = (TextView) mLayoutView.findViewById(R.id.frag_webview_tv_toolbar_title);
        mToolbarBottomLine = mLayoutView.findViewById(R.id.frag_webview_view_toolbar_bottom_line);
        mRefreshLayout = (RefreshLayout) mLayoutView.findViewById(R.id.frag_webview_refresh_layout);
        mLayoutWebView = (LinearLayout) mLayoutView.findViewById(R.id.frag_webview_layout);

        mLoadingView = (AVLoadingIndicatorView) mLayoutView.findViewById(R.id.frag_webview_loading);
        mRelativeLayout = (RelativeLayout) mLayoutView.findViewById(R.id.frag_webview_rlayout);

        mLayoutSearch = (LinearLayout) mLayoutView.findViewById(R.id.frag_webview_layout_search);
        mLayoutSearch.setVisibility(View.GONE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case Constants.ACTIVITY_CODE.RESULT_REFRESH:
                if (Utils.Network.isNetworkAvailable()) {
                    mWebViewManager.reload();
                } else {
                    Utils.UI.showToastLong(strNoInternet);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

     @Background(delay = 2000)
    public void delayToHideLoadingView() {
        hideLoadingView();
    }

    @UiThread
    void hideLoadingView() {
        if (mRelativeLayout != null && mLoadingView != null) {
            mRelativeLayout.setVisibility(View.VISIBLE);
            mLoadingView.setVisibility(View.GONE);
        }
    }

}
